#pragma once

#include <vector>
#include <unordered_map>
using namespace std;

class Solution
{
public:
    int totalFruit(vector<int>& fruits)
    {
        int n = fruits.size();
        unordered_map<int, int> hash;

        if (fruits.size() == 1)return 1;
        int ret = 0;
        int left = 0, right = 0;
        ++hash[fruits[right++]];
        while (left < n && right < n)
        {
            ++hash[fruits[right]];
            while (hash.size() > 2)
            {
                --hash[fruits[left]];
                if (hash[fruits[left]] == 0)hash.erase(fruits[left]);
                ++left;
            }
            if (right - left + 1 > ret) ret = right - left + 1;
            ++right;
        }
        return ret;
    }
};

class Direct
{
public:
    int totalFruit(vector<int>& fruits)
    {
        int n = fruits.size();
        unordered_map<int, int> hash;

        if (fruits.size() == 1)return 1;
        int ret = 0;

        for (int i = 0; i < n; ++i)
        {
            for (int j = i; j < n; ++j)
            {
                ++hash[fruits[j]];
                if (hash.size() > 2)break;
                if (ret < j - i + 1) ret = j - i + 1;
            }
            hash.clear();
        }
        return ret;
    }
};